var Cookies = {
  set: function(key, value, expiry) {
    value = value.toJSON();
    if (!expiry) {
       expiry = this._createExpiryDate(86400);
    }
    else if (typeof expiry == 'number') {
      expiry = this._createExpiryDate(expiry);
    }
    document.cookie = key + "=" + value + '; expires=' + expiry.toGMTString() + "; path=/";
  },
  get: function(key) {
    var cookies = document.cookie.split(';');
    key = key + '=';
    value = null;
    cookies.each(function(cookie) {
      cookie = cookie.strip();
      if (cookie.startsWith(key)) {
        value = this._evalAndExtend(cookie.substring(key.length, cookie.length)); 
      }
    }.bind(this));
    return value;
  },
  remove: function(key) {
    this.set(key, '', -1);
  },
  _createExpiryDate: function(expiry) {
    var expiryDate = new Date();
    expiryDate.setTime(expiryDate.getTime() + (expiry * 1000));
    return expiryDate;
  },
  _evalAndExtend: function(value) {
    var object = value.evalJSON();
    switch(value[0]) {
      case '[': return $A(object);
      case '{': return $H(object);
      default:  return object;
    }
  }
};